import { applyDecorators, UseInterceptors } from '@nestjs/common'
import { FileFastifyInterceptor } from 'fastify-file-interceptor'
import multer from 'multer'

export function ApiFile(
  fieldName = 'file',
  localOptions?: multer.Options,
) {
  return applyDecorators(
    UseInterceptors(FileFastifyInterceptor(fieldName, localOptions)),
  )
}

export function getFileBlobMimeType(buff: Buffer): string {
  const arr = (new Uint8Array(buff)).subarray(0, 16)
  let header = ''
  for (let i = 0; i < arr.length; i++) {
    header += arr[i].toString(16)
  }

  let result = 'unknown'

  switch (true) {
    case header.startsWith('89504e47'): // png apng
      result = 'image/png'
      break
    case header.startsWith('47494638'): // gif
      result = 'image/gif'
      break
    case header.startsWith('ffd8ffe0'): // jpg
    case header.startsWith('ffd8ffe1'): // jpg
    case header.startsWith('ffd8ffe2'): // jpg
    case header.startsWith('ffd8ffe3'): // jpg
    case header.startsWith('ffd8ffe8'): // jpg
      result = 'image/jpeg'
      break
    case header.endsWith('66747970617669660000'): // avif
    case header.endsWith('667479706d6966310000'): // mif1 HEIF avif
      result = 'image/avif'
      break
    case header.endsWith('66747970617669730000'): // avif sequence
      result = 'image/avifs'
      break
    case !!header.match(/52494646[a-z-A-Z-0-9]*57454250/g): // webp
      result = 'image/webp'
      break
  }

  return result
}
